﻿//********************************************************************************//
//创建日期：<创建日期，2016-04-11>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：打印通知单
//********************************************************************************//
layui.use(['form', 'jquery', 'laydate', 'upload'], function () {
    var form = layui.form
    , layer = layui.layer
    , $ = layui.jquery
    , laydate = layui.laydate
    , upload = layui.upload;
    /*initial load start*/
    initcontrol();
    load();
    /*initial load end*/

    /*initial*/
    function initcontrol() {
        //加载部门
        //$("#cBM option").remove();
        var list = getYSBMList("");
        //$("#cBM").append("<option value='0'>  </option>");
        for (var i = 0; i < list.length; i++) {
            $("#cBM").append("<option value='" + list[i]["Value"] + "'>" + list[i]["Text"] + "</option>");
        }
        $("#cBM").get(0).selectedIndex = 0;
        $("#iBM option").remove();
        for (var i = 0; i < list.length; i++) {
            $("#iBM").append("<option value='" + list[i]["Value"] + "'>" + list[i]["Text"] + "</option>");
        }
        $("#iBM").get(0).selectedIndex = 0;
        form.render("select");
    }
    /*end initial*/

    /*event*/
   
    $("#oSearch").click(function () {
        load($("#cName").val(), $("#cBM").val(), $("#cActive").val());
    });
    $("#iBtnViewRole").click(function () {
        location.href = 'UserRoleConfig.aspx';
    });
    $("#iBtnAdd").click(function () {
        $("#lsOperateInfo").attr("vid2", "add");
        clearinfo();
        editMode();
        $("#iAccount").removeAttr("disabled");
        $("#iAccount").css("background-color", "");
        $("#iBtnAdd").hide();
        $("#iBtnEdit").hide();
        $("#iBtnSave").show();
        $("#iBtnCancel").show();
        $("#iAccount").focus();
    });
    $("#iBtnEdit").click(function () {
        if ($("#lsOperateInfo").attr("vid") == "0")
        {           
            return;
        }
        $("#lsOperateInfo").attr("vid2", "edit");
        editMode(); 
    });
    $("#iBtnCancel").click(function () {
        $("#lsOperateInfo").attr("vid2", "");
        $("#ContentList1 .datarow[vid='" + $("#lsOperateInfo").attr("vid") + "']").click();
        initialMode();       
    });

    $("#iBtnSave").click(function () {
        if (verify())
        {            
            var index = layer.load(1);
            $.ajax({
                type: "post",
                cache: false,
                async: false,
                url: "/ashx/XT/XT.ashx",
                data: {
                    "op": "yhxx_save",
                    "mode": $("#lsOperateInfo").attr("vid2"),
                    "iAccount": $("#iAccount").val(),
                    "iUserName": $("#iUserName").val(),
                    "iActive": $("#iActive").val(),
                    "iBM": $("#iBM").val(),
                    "iTelephone": $("#iTelephone").val(),
                    "iMobilephone": $("#iMobilephone").val(),
                    "iEmail": $("#iEmail").val(),
                    "iID_card": $("#iID_card").val(),
                    "iBirthday": $("#iBirthday").val(),
                    "iGender": $("#iGender").val(),
                    "iAddress": $("#iAddress").val(),
                    "iPostcode": $("#iPostcode").val(),
                    "iDesc": $("#iDesc").val()
                },
                dataType: "json",
                success: function (rs) {
                    layer.msg(rs.Msg);
                    load();
                    listCover(false, "divList");
                    clearinfo();
                    initialMode();
                },
                error: function (error) {
                    layer.msg(error.responseText);
                }
            });
            layer.close(index);
        }        
    });

    /*event end*/

    /*list*/
    function createItemStr(obj) {
        var str = $("#ItemStr1 tr").prop("outerHTML");
        var itemstr, userid, username, account,department,sfsc;
        userid = obj["USERID"];
        username = obj["USERNAME"];
        account = obj["ACCOUNT"];
        department = obj["DEPARTMENTNAME"];
        sfsc = obj["SFSC"];
        itemstr = str.replace(new RegExp("objUserId", "gm"), userid)
                        .replace(new RegExp("objUserName", "gm"), username)
                        .replace(new RegExp("objAccount", "gm"), account)
                        .replace(new RegExp("objDepartmentName", "gm"), department)
                        .replace(new RegExp("objSC", "gm"), sfsc)
        ;
        return itemstr;
    }
    function bindListOperate()
    {
        $("#ContentList1 .datarow").unbind("click").click(function () {
            //右侧未开启编辑模式时,左侧点击可变更右侧信息          
                //清除之前选择项变色
                $("#ContentList1 .datarow[vid='" + $("#lsOperateInfo").attr("vid") + "']").css("background-color", "");

                //记录当前选择项
                var userid = $(this).attr("vid");
                $($("#lsOperateInfo").attr("vid", userid));
                //将当前选择项变色
                $("#ContentList1 .datarow[vid='" + userid + "']").css("background-color", "lightgray");
                //加载右侧详细信息
                loadInfo(userid);

            //点击后，让上传电子签名按钮变亮
                $('#ofDR').removeAttr("disabled");
                $('#ofDR').css('background-color','white');

        });
    }
    /**
   
    **/
    function load() {
        var name = $("#cName").val();
        var bm = $("#cBM").val();
        var active = $("#cActive").val();
        var index = layer.load(1);
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/XT/XT.ashx",
            data: {
                "op": "yhxx",
                "name": name,
                "bm": bm=="0"?"-1":bm,
                "active": active == "0" ? "-1" : active == "1"?"1":"0"
            },
            dataType: "json",
            success: function (rs) {
                
                var strs = "";
                for (var i in rs) {
                    strs = strs + createItemStr(rs[i]);
                }
                $("#ContentList1").find(".datarow").remove();
                $("#ContentList1").append(strs);
                $("#lsCount").text(rs.length);
                bindListOperate();
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
        layer.close(index);
    }

    function loadInfo(id)
    {
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/XT/XT.ashx",
            data: {
                "op": "yhxx_detail",
                "id": id
            },
            dataType: "json",
            success: function (rs) {
                $("#iAccount").val(rs["ACCOUNT"]);
                $("#iAddress").val(rs["ADDRESS"]);
                $("#iBirthday").val(rs["BIRTHDAY"]);
                $("#iBM").val(rs["DEPARTMENT"]);
                $("#iDesc").val(rs["USERDESC"]);
                $("#iEmail").val(rs["EMAIL"]);
                $("#iGender").val(rs["GENDER"]);
                $("#iID_card").val(rs["IDCARD"]);
                $("#iActive").val(rs["ISACTIVE"]);
                $("#iMobilephone").val(rs["MOBILEPHONE"]);
                //$("#iPassword").val(rs["PASSWORD"]);
                $("#iPostcode").val(rs["POSTCODE"]);
                $("#iTelephone").val(rs["TELEPHONE"]);
                $("#iUserName").val(rs["USERNAME"]);
                form.render("select");
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
    }

    /*end list*/

    /*logic*/

    function initialMode()
    {
        $("#iAccount").attr("disabled", "disabled");
        $("#iAccount").css("background-color", "lightgray");
        $("#iUserName").attr("disabled","disabled");
        $("#iPassword").attr("disabled", "disabled");
        $("#iActive").attr("disabled", "disabled");
        $("#iBM").attr("disabled", "disabled");
        $("#iTelephone").attr("disabled", "disabled");
        $("#iMobilephone").attr("disabled", "disabled");
        $("#iEmail").attr("disabled", "disabled");
        $("#iID_card").attr("disabled", "disabled");
        $("#iBirthday").attr("disabled", "disabled");
        $("#iGender").attr("disabled", "disabled");
        $("#iAddress").attr("disabled", "disabled");
        $("#iPostcode").attr("disabled", "disabled");
        $("#iDesc").attr("disabled", "disabled");
        listCover(false, "divList");
        $("#iBtnAdd").show();
        $("#iBtnEdit").show();
        $("#iBtnSave").hide();
        $("#iBtnCancel").hide();
        form.render("select");
    }
    function editMode() {
        $("#iUserName").removeAttr("disabled");
        $("#iPassword").removeAttr("disabled");
        $("#iActive").removeAttr("disabled");
        $("#iBM").removeAttr("disabled");
        $("#iTelephone").removeAttr("disabled");
        $("#iMobilephone").removeAttr("disabled");
        $("#iEmail").removeAttr("disabled");
        $("#iID_card").removeAttr("disabled");
        $("#iBirthday").removeAttr("disabled");
        $("#iGender").removeAttr("disabled");
        $("#iAddress").removeAttr("disabled");
        $("#iPostcode").removeAttr("disabled");
        $("#iDesc").removeAttr("disabled");
        listCover(true, "divList");
        $("#iBtnAdd").hide();
        $("#iBtnEdit").hide();
        $("#iBtnSave").show();
        $("#iBtnCancel").show();
        form.render("select");
    }

    function clearinfo()
    {
        $("#iAccount").val("");
        $("#iUserName").val("");
        $("#iPassword").val("");
        $("#iTelephone").val("");
        $("#iMobilephone").val("");
        $("#iEmail").val("");
        $("#iID_card").val("");
        $("#iBirthday").val("");
        $("#iAddress").val("");
        $("#iPostcode").val("");
        $("#iDesc").val("");
        form.render("select");
    }
    function verify() {
        if (!verEmpty($("#iAccount").val()))
        {
            layer.alert("请填写账号!", function (index) {
                $("#iAccount").focus();
                layer.close(index);
            });
            return false;
        }
        if (!verEmpty($("#iUserName").val())) {
            layer.alert("请填写用户名!", function (index) {
                $("#iUserName").focus();
                layer.close(index);
            });            
            return false;
        }
        if ($("#iBM").val() == "0" || $("#iBM").val()==null) {
            layer.alert("请选择部门!", function (index) {
                $("#iBM").focus();
                layer.close(index);
            });            
            return false;
        }
        return true;
    }

   
    var urls = '';
    $("#iBtnUpload").click(function () {
        var userid = $("#iAccount").val();
        if (userid == '') {
            alert('请选择用户');
        }
        else {
            urls = '/ashx/XT/XT.ashx?op=SCDZQM&UserID=' + $("#iAccount").val();
            $("#ofDR").click();
            //userID = $("#iAccount").val();
            
        }

    });
   // localStorage.setItem("account", "3");

   

    upload.render({
        elem: '#ofDR',
        url: '/ashx/XT/XT.ashx?op=SCDZQM',//&UserID=' + $("#iAccount").val(),
        dataType: "json",
        //data: {"op":"SCDZQM",
        //    "UserID": $("#ipt_hid").val()
        //},
        accept: 'images',
        method: 'post',
        size: '200',
        before: function (input) {
            //alert($("#iAccount").val());
            //$('.layui-upload-file').after('<input type="hidden" id="ipt_hid"  value='$('#iAccount').val()' />');
            this.data = { "UserID": $("#iAccount").val() };

            //var userid = $("#iAccount").val();
            //if (userid == '') {
            //    alert('请选择用户');
            //    return;
            //}
        },
        done: function (res, index, upload) {
            if (res.State) {
                alert(res.Msg + '导入成功');
                load();
            }
            else {
                layer.alert(res.Msg + '导入失败');
            }
        }
    });

    /*logic end*/
});
